﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;

namespace Cheems.Control.Controls
{
    public class RoundedCornerGrid : Grid
    {
        public RoundedCornerGrid()
        {
            // 订阅 SizeChanged 事件
            this.SizeChanged += OnSizeChanged;
        }

        private void OnSizeChanged(object sender, SizeChangedEventArgs e)
        {
            // 当控件的宽度或高度变化时，触发此事件
            double newWidth = e.NewSize.Width;
            double newHeight = e.NewSize.Height;

            // 在这里执行你希望在大小变化时进行的操作
            // 例如，根据新的宽度和高度更新圆角等
            OnCornerRadiusChanged(this, new DependencyPropertyChangedEventArgs());
        }

        public CornerRadius CornerRadius
        {
            get { return (CornerRadius)GetValue(CornerRadiusProperty); }
            set { SetValue(CornerRadiusProperty, value); }
        }
        public static readonly DependencyProperty CornerRadiusProperty =
            DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(RoundedCornerGrid), new PropertyMetadata(new CornerRadius(0), OnCornerRadiusChanged));

        /// <summary>
        /// 圆角变化时触发
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        /// <exception cref="NotImplementedException"></exception>
        private static void OnCornerRadiusChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            RoundedCornerGrid thisControl = d as RoundedCornerGrid;
            if (thisControl.ActualHeight == 0 || thisControl.ActualWidth == 0)
            {
                return;
            }
            // 参数计算
            double max = Math.Min(thisControl.ActualWidth, thisControl.ActualHeight) / 2;
            double topleft = (thisControl.CornerRadius.TopLeft < max) ? thisControl.CornerRadius.TopLeft : max;
            double topright = (thisControl.CornerRadius.TopRight < max) ? thisControl.CornerRadius.TopRight : max;
            double bottomright = (thisControl.CornerRadius.BottomRight < max) ? thisControl.CornerRadius.BottomRight : max;
            double bottomleft = (thisControl.CornerRadius.BottomLeft < max) ? thisControl.CornerRadius.BottomLeft : max;
            Point point1 = new Point(0, topleft);
            Point point2 = new Point(topleft, 0);
            Point point3 = new Point(thisControl.ActualWidth - topright, 0);
            Point point4 = new Point(thisControl.ActualWidth, topright);
            Point point5 = new Point(thisControl.ActualWidth, thisControl.ActualHeight - bottomright);
            Point point6 = new Point(thisControl.ActualWidth - bottomright, thisControl.ActualHeight);
            Point point7 = new Point(bottomleft, thisControl.ActualHeight);
            Point point8 = new Point(0, thisControl.ActualHeight - bottomleft);

            // 创建 PathGeometry
            PathGeometry pathGeometry = new PathGeometry();

            // 创建 PathFigure
            PathFigure pathFigure = new PathFigure
            {
                StartPoint = point1, // 起始点
                IsClosed = true // 闭合路径
            };
            // 点2：圆弧连接
            pathFigure.Segments.Add(new ArcSegment(point2, new Size(topleft, topleft), 0, false, SweepDirection.Clockwise, true));
            pathFigure.Segments.Add(new LineSegment(point3, true));
            pathFigure.Segments.Add(new ArcSegment(point4, new Size(topright, topright), 0, false, SweepDirection.Clockwise, true));
            pathFigure.Segments.Add(new LineSegment(point5, true));
            pathFigure.Segments.Add(new ArcSegment(point6, new Size(bottomright, bottomright), 0, false, SweepDirection.Clockwise, true));
            pathFigure.Segments.Add(new LineSegment(point7, true));
            pathFigure.Segments.Add(new ArcSegment(point8, new Size(bottomleft, bottomleft), 0, false, SweepDirection.Clockwise, true));
            pathFigure.Segments.Add(new LineSegment(point1, true));

            // 将 PathFigure 添加到 PathGeometry
            pathGeometry.Figures.Add(pathFigure);

            // 将 PathGeometry 设置为 Grid 的 Clip
            thisControl.Clip = pathGeometry;
        }
    }
}
